# -*- coding: utf-8 -*-
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
import sys
import math

class CircleDialog(QWidget):
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)

        self.setWindowTitle(self.tr("计算圆形面积"))
        self.resize(400,150)

        radisLabel=QLabel(self.tr("半径："))
        self.radisLEdit=QLineEdit()
        areaLabel=QLabel(self.tr("面积："))
        self.areaLEdit=QLineEdit()
        self.areaLEdit.setReadOnly(True)

        self.calcButton=QPushButton(self.tr("计算"))


        contLayout=QGridLayout()
        contLayout.addWidget(radisLabel,0,0)
        contLayout.addWidget(self.radisLEdit,0,1)
        contLayout.addWidget(areaLabel,0,2)
        contLayout.addWidget(self.areaLEdit,0,3)
        contLayout.addWidget(self.calcButton,1,3)
        contLayout.setSpacing(10)
        contLayout.setMargin(10)
        #绑定点事件
        self.connect(self.calcButton,SIGNAL("clicked()"),self.okClicked)
        self.calcButton.setShortcut(QKeySequence(Qt.Key_Enter))
        self.calcButton.setShortcut(QKeySequence(Qt.Key_Return))

        self.setLayout(contLayout)

    def okClicked(self):
        radisValStr=str(self.radisLEdit.text())
        print(radisValStr)
        if radisValStr !='':
            if str.isdigit(radisValStr)==True:
                radisValInt=int(radisValStr)
                area=radisValInt * radisValInt * math.pi;
                self.areaLEdit.setText(str(round(area,2)))
        else:
            self.areaLEdit.setText("")



if __name__=="__main__":
    app=QApplication(sys.argv)
    app.setStyleSheet('''
        QPushButton{
            background-color:#0f0;
            height:30px;
            border-style:outset;
            border-width:2px;
            border-radius:10px;
            border-color:beige;
            font:bold 14px;
            padding:6px;
        }
        QPushButton:hover{
            background-color:red;
            border-style:insert;
        }
        QPushButton:pressed{
            background-color:rgb(224,0,0);
            border-style:insert;
        }
    ''')
    circleDialog=CircleDialog()
    circleDialog.show()
    sys.exit(app.exec_())